package top.ahzy.seobench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ahzy.seobench.mapper.ConfigDataMapper;
import top.ahzy.seobench.mapper.UserMapper;
import top.ahzy.seobench.model.entity.ConfigData;
import top.ahzy.seobench.model.entity.User;
import top.ahzy.seobench.model.req.PageParam;
import top.ahzy.seobench.model.req.UserEditReq;
import top.ahzy.seobench.model.req.UserLoginReq;
import top.ahzy.seobench.model.rsp.UserLoginRsp;
import top.ahzy.seobench.result.Result;
import top.ahzy.seobench.result.ResultList;
import top.ahzy.seobench.service.UserService;
import top.ahzy.seobench.util.DefaultDataUtil;
import top.ahzy.seobench.util.JwtUtil;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    ConfigDataMapper configDataMapper;

    @Autowired
    DefaultDataUtil defaultDataUtil;
    @Override
    public Result<UserLoginRsp> login(UserLoginReq param) {
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername,param.getUsername())
                .eq(User::getPassword,param.getPassword());
        User user = userMapper.selectOne(userWrapper);
        if(ObjUtil.isNull(user)){
            return Result.error("用户账号或密码错误!");
        }

        UserLoginRsp vo = new UserLoginRsp();
        vo.setToken(JwtUtil.createAccessToken(user));
        LambdaQueryWrapper<ConfigData> configDataWrapper = new LambdaQueryWrapper<>();
        configDataWrapper.eq(ConfigData::getUserId,user.getId())
                .orderByDesc(ConfigData::getCreateTime)
                .last("LIMIT 1");
        ConfigData configData = configDataMapper.selectOne(configDataWrapper);
        if(ObjUtil.isNull(configData)){
            vo.setData(defaultDataUtil.get());
        }else {
            vo.setData(configData.getDetail());
        }

        return Result.success(vo);
    }

    @Override
    public Result<String> out() {
        return Result.success("退出成功");
    }

    @Override
    public Result<String> edit(UserEditReq param) {
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername,param.getUsername());
        User user = userMapper.selectOne(userWrapper);
        if(StrUtil.isEmpty(param.getId())){
            if(ObjUtil.isNotNull(user)){
                return Result.error("用户名已存在!");
            }

            user = BeanUtil.copyProperties(param,User.class);
            user.setId(IdUtil.simpleUUID());
            user.setSalt(user.getId());
            user.setStatus(1);
            user.setSex(1);
            user.setCreateTime(new Date());
            userMapper.insert(user);
            return Result.success("新增成功");
        }else {
            if(ObjUtil.isNull(user)){
                return Result.error("用户名不存在!");
            }

            user.setPassword(param.getPassword());
            userMapper.updateById(user);
            return Result.success("编辑成功");

        }
    }

    @Override
    public Result<ResultList<User>> list(PageParam param) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        Page<User> page = new Page<>(param.getPage(),param.getSize());
        page = userMapper.selectPage(page, wrapper);
        return new Result().successInstanceof(page);
    }


}
